<template>
  <div class="login-container">
    <!-- 登录卡片 -->
    <div class="login-card">
      <div class="login-header">
        <div class="logo-section">
          <img src="@/assets/logo.png" alt="Logo" class="logo" />
          <h1 class="title">Vue3 Admin</h1>
        </div>
        <p class="subtitle">欢迎回来，请登录您的账户</p>
      </div>
      
      <el-form
        class="login-form"
        :model="form"
        @submit="handleSubmit"
        size="large"
      >
        <el-form-item>
          <el-input
            v-model="form.userAccount"
            placeholder="请输入账号"
            :prefix-icon="User"
            class="login-input"
          />
        </el-form-item>
        
        <el-form-item>
          <el-input
            v-model="form.userPassword"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            class="login-input"
            show-password
          />
        </el-form-item>
        
        <div class="form-options">
          <!--<el-checkbox v-model="rememberMe">记住我</el-checkbox>-->
          <a href="#" class="forgot-password">忘记密码？</a>
        </div>
        
        <el-form-item>
          <el-button
            type="primary"
            class="login-button"
            :loading="loading"
            @click.prevent="handleSubmit"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <!--<div class="login-footer">-->
      <!--  <p class="footer-text">-->
      <!--    还没有账户？-->
      <!--    <a href="#" class="register-link">立即注册</a>-->
      <!--  </p>-->
      <!--</div>-->
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { getCurrentUser, login } from "@/api/user";
import { ElMessage } from 'element-plus';

/**
 * 表单信息
 */
const form = reactive({
  userAccount: "admin",
  userPassword: "123456",
} as any);

const router = useRouter();
const store = useStore();
const loading = ref(false);
const rememberMe = ref(false);

/**
 * 提交表单
 */
const handleSubmit = async () => {
  loading.value = true;
  try {
    const res = await login(form);
    // 登录成功，跳转到主页
    if (res.code === 0) {
      const loginRes = await getCurrentUser();
      if (loginRes.code === 0) {
        store.commit("updateUser", loginRes.data)
      }
      
      router.push({
        path: "/",
        replace: true,
      });
    } else {
      ElMessage.error("登录失败，" + res.message);
    }
  } catch (error) {
    ElMessage.error("登录失败，请检查网络连接");
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(90deg, rgb(223, 236, 254) 0%, rgb(252, 242, 248) 50%, rgb(207, 226, 241) 80%);
  position: relative;
  overflow: hidden;
  margin: 0;
  padding: 0;
}

/* 背景装饰 */
.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.circle-2 {
  width: 150px;
  height: 150px;
  top: 60%;
  right: 10%;
  animation-delay: 2s;
}

.circle-3 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

/* 登录卡片 */
.login-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 40px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  animation: slideUp 0.6s ease-out;
  margin: 0;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 登录头部 */
.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-section {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
}

.logo {
  width: 40px;
  height: 40px;
  margin-right: 12px;
}

.title {
  font-size: 28px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: #7f8c8d;
  font-size: 16px;
  margin: 0;
  font-weight: 400;
}

/* 登录表单 */
.login-form {
  margin-bottom: 20px;
}

.login-input {
  border-radius: 12px;
  border: 2px solid #e1e8ed;
  transition: all 0.3s ease;
}

.login-input:hover {
  border-color: #667eea;
}

.login-input :deep(.el-input__wrapper) {
  background: #f8f9fa;
  border: none;
  box-shadow: none;
  padding: 12px 16px;
}

.login-input :deep(.el-input__wrapper:hover) {
  background: #fff;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
}

.login-input :deep(.el-input__wrapper.is-focus) {
  background: #fff;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.3);
}

.login-input :deep(.el-input__inner) {
  font-size: 16px;
  color: #2c3e50;
}

.login-input :deep(.el-input__prefix) {
  color: #667eea;
}

/* 表单选项 */
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.forgot-password {
  color: #667eea;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: #764ba2;
  text-decoration: underline;
}

/* 登录按钮 */
.login-button {
  width: 100%;
  height: 48px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.login-button:active {
  transform: translateY(0);
}

/* 登录底部 */
.login-footer {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #e1e8ed;
}

.footer-text {
  color: #7f8c8d;
  font-size: 14px;
  margin: 0;
}

.register-link {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
  transition: color 0.3s ease;
}

.register-link:hover {
  color: #764ba2;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-card {
    margin: 20px;
    padding: 30px 20px;
  }
  
  .title {
    font-size: 24px;
  }
  
  .subtitle {
    font-size: 14px;
  }
  
  .login-input :deep(.el-input__wrapper) {
    padding: 10px 14px;
  }
  
  .login-button {
    height: 44px;
    font-size: 15px;
  }
}

/* 深色模式支持 */
@media (prefers-color-scheme: dark) {
  .login-card {
    background: rgba(30, 30, 30, 0.95);
    border-color: rgba(255, 255, 255, 0.1);
  }
  
  .title {
    color: #fff;
  }
  
  .subtitle {
    color: #bdc3c7;
  }
  
  .login-input :deep(.el-input__wrapper) {
    background: rgba(255, 255, 255, 0.05);
  }
  
  .login-input :deep(.el-input__wrapper:hover) {
    background: rgba(255, 255, 255, 0.1);
  }
  
  .login-input :deep(.el-input__inner) {
    color: #fff;
  }
  
  .footer-text {
    color: #bdc3c7;
  }
}
</style>
